//
//  SuggestionViewController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/11/18.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "SuggestionViewController.h"

@interface SuggestionViewController ()<UITextViewDelegate>
{
    NSNumber *_type;
}
@property (weak, nonatomic) IBOutlet UITextView *suggestionLv;
@property (weak, nonatomic) IBOutlet UIView *backView;
@end

@implementation SuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _suggestionLv.layer.borderWidth = 1;
    _suggestionLv.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:0.5].CGColor;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - keyboard events -

///键盘显示事件
- (void) keyboardWillShow:(NSNotification *)notification {
//获取键盘高度，在不同设备上，以及中英文下是不同的
    CGFloat kbHeight = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;

//计算出键盘顶端到inputTextView panel底端的距离(加上自定义的缓冲距离INTERVAL_KEYBOARD)
    CGFloat offset = kbHeight - (HEIGHT - _suggestionLv.frame.origin.y - _suggestionLv.frame.size.height) + 80;

    NSLog(@"%F %F %F",kbHeight,_suggestionLv.frame.origin.y,_suggestionLv.frame.size.height);
    
// 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

//将视图上移计算好的偏移
    if(offset > 0) {
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
 }];
}
}

///键盘消失事件
- (void) keyboardWillHide:(NSNotification *)notify {
    // 键盘动画时间
    double duration = [[notify.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
        //视图下沉恢复原状
        [UIView animateWithDuration:duration animations:^{
            self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

- (IBAction)btnClick:(UIButton *)sender {
    
    for (int i = 0; i < 5; i++) {
        UIButton *btn = [_backView viewWithTag:100+i];
        btn.selected = NO;
    }
    
    _type = @(sender.tag-100);
    sender.selected = YES;
}


- (IBAction)submitClick:(UIButton *)sender {
    
    NSString *userid = @"";
    NSString *username = @"";
    NSString *mac = @"";
    
    if (!_suggestionLv.text.length) {
        [self.view Message:@"请输入反馈内容" HiddenAfterDelay:1];
        return;
    }
    if (!_type) {
        [self.view Message:@"请选择类型" HiddenAfterDelay:1];
        return;
    }
    if (USERID) {
        userid = USERID;
    }
    if (USERNAME) {
        username = USERNAME;
    }
    if (MAC) {
        mac = MAC;
    }
    
    NSDictionary *parameter = @{@"content":_suggestionLv.text,@"create_time":[MyDateformater getCurrentDate],@"type":_type,@"user_id":userid,@"user_name":username,@"mac":mac};
    
    [MyAFN POST:FEEDBACK parameters:parameter block:^(id responseObject) {
        
        NSDictionary *response = responseObject;
        if ([[response objectForKey:@"status"]integerValue]) {
            
            [self performSelector:@selector(showMsg) withObject:nil afterDelay:0.5];
        }
        
    } error:^(NSError *error) {
        
    } fromController:self];
}

- (void)showMsg
{
    [self.view Message:@"感谢您的反馈" HiddenAfterDelay:1];
    [self performSelector:@selector(back) withObject:nil afterDelay:1];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view != _suggestionLv) {
        [self.view endEditing:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
